home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / emailAddress.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  4.2 KB  |  121 lines

  1. #
  2. #  emailAddress.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 2/1/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. #!/usr/bin/env python
  24.  
  25. from consts import *
  26. from GlobalObjects import *
  27.  
  28.  
  29. def validateEmailAddress (addr):
  30.     """Returns tuple (email address, domain) if successful (both in lower case),
  31.     otherwise returns (False, False)"""
  32.     mo = globalObjects.emailAddrPat.match(addr)
  33.     if mo:
  34.         # first possibility: plain email address
  35.         domain = mo.group(2)
  36.         if domain:
  37.             # rid of comments
  38.             return globalObjects.emailAddrCommentPat.sub('', mo.group(1)).lower(), globalObjects.emailAddrCommentPat.sub('', domain).lower()
  39.  
  40.         # second possibility: in the form of "Name <id@domain>"
  41.         domain = mo.group(4)
  42.         if domain:
  43.             # rid of comments
  44.             return globalObjects.emailAddrCommentPat.sub('', mo.group(3)).lower(), globalObjects.emailAddrCommentPat.sub('', domain).lower()
  45.         return False, False
  46.     
  47.     return False, False
  48.  
  49. def extractEmailAddresses (s):
  50.     """Extract email addresses and domains from string s:
  51.  
  52.     1. If successful returns a list of tuples (text, email address, domain), where
  53.        text is the original text of the address (could be in different encoding and/or could
  54.        include names);
  55.     2. If failed returns a string that's skipped (the error is usually around the text).
  56.     """
  57.     ret = []
  58.     first = True
  59.     s = s.strip()
  60.     sSize = len(s)
  61.     if sSize == 0: return ret
  62.     
  63.     mo = None
  64.     for mo in globalObjects.emailAddrPat.finditer(s):
  65.         if first:
  66.             if mo.start(0) != 0:
  67.                 # something is skipped
  68.                 return s[0:mo.start(0)]
  69.             first = False
  70.         else:
  71.             if lastEnd != mo.start(0):
  72.                 # something is skipped
  73.                 return s[lastEnd:mo.start(0)]
  74.  
  75.         lastEnd = mo.end(0)
  76.  
  77.         text = mo.group(0).strip()
  78.         if text[-1] == ',':
  79.             text = text[:-1]
  80.         
  81.         # first possibility: plain email address
  82.         domain = mo.group(2)
  83.         if domain:            
  84.             ret.append((text,
  85.                         globalObjects.emailAddrCommentPat.sub('', mo.group(1)).lower(),
  86.                         globalObjects.emailAddrCommentPat.sub('', domain).lower()))
  87.             
  88.         else:
  89.             # second possibility: in the form of "Name <id@domain>"
  90.             ret.append((text,
  91.                         globalObjects.emailAddrCommentPat.sub('', mo.group(3)).lower(),
  92.                         globalObjects.emailAddrCommentPat.sub('', mo.group(4)).lower()))
  93.  
  94.     if mo:
  95.         if mo.end(0) != sSize:
  96.             # something is skipped
  97.             return s[mo.end(0):sSize]
  98.     else:
  99.         return s
  100.  
  101.     return ret
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     print validateEmailAddress('foo@bar.com')
  106.     print validateEmailAddress('Foo <foo@bar.com>')
  107.     print validateEmailAddress('"Foo" <foo@bar.com>')
  108.  
  109.     print validateEmailAddress('foo@')
  110.     print validateEmailAddress('@bar.com')
  111.     print validateEmailAddress('"Foo" <foo@>')
  112.     
  113.     print validateEmailAddress('\'"Kent, Clark\'" <superman@krypton.net>')
  114.  
  115.     print validateEmailAddress('someone@foo.com<someone@foo.com>')
  116.  
  117.     print extractEmailAddresses('   "Foo, Bar" <foo@bar.com>, Foo1 <FOO1@bar.com>, foo2@bar.com')
  118.     print extractEmailAddresses('   "Foo, Bar" <foo@bar.com>, Foo1 FOO1@bar.com>, foo2@bar.com')
  119.     print extractEmailAddresses('   "Foo, Bar" <foo@bar.com>, Foo1 <FOO1@bar.com>, foo2')
  120.     print extractEmailAddresses('   "Foo, Bar" <foo@bar.com>, <foo1@>, foo2@bar.com')
  121.